//
//  JXTabBarVC.swift
//  WBTest
//
//  Created by Fantasy on 2016/12/8.
//  Copyright © 2016年 Fantasy. All rights reserved.
//

import UIKit

class JXTabBarVC: UITabBarController {

    // 懒加载是! 的 所以必须定义时给类型
    lazy var composeBtn: UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
//    lazy var composeBtn: UIButton = self.test(imageName: "tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 设置tabbar的navigation 和使用反射
        setupChildController()
        setupComposeBtn()
    }
    
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
    
    // 按钮点击
    @objc func composeBtnClick() {
        print(#function)
    }
}

// 用于分离代码
extension JXTabBarVC {
    
    func setupComposeBtn() {
        tabBar.addSubview(composeBtn)
        
        // 计算按钮宽度
        let count = CGFloat(childViewControllers.count)
        let w = tabBar.bounds.width / count - 1
        composeBtn.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        composeBtn.addTarget(self, action: #selector(composeBtnClick), for: .touchUpInside)
    }
    
    func setupChildController() {
        let array: [[String : Any]] = [
//            ["clsName" : "JXHomeVC", "title" : "首页", "imageName" : "home"],
            ["clsName" : "JXHomeVC", "title" : "首页", "imageName" : "home", "visitorInfo" : ["imageName" : "", "message" : "关注一些人, 回这里看看有什么惊喜"]],
            ["clsName" : "JXMessageVC", "title" : "消息", "imageName" : "message_center", "visitorInfo" : ["imageName" : "visitordiscover_image_message", "message": "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知"]],
            // 中间挤开tabBar的新技巧
            ["clsName" : "UIViewController"],
            ["clsName" : "JXDiscoverVC", "title" : "发现", "imageName" : "discover",  "visitorInfo": ["imageName": "visitordiscover_image_message", "message": "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过"]],
            ["clsName" : "JXProfileVC", "title" : "我的", "imageName" : "profile", "visitorInfo": ["imageName": "visitordiscover_image_profile", "message": "登录后，你的微博、相册、个人资料会显示在这里，展示给别人"]]
        ]
        
//        (array as NSArray).write(toFile: "/Users/macong/Desktop/demo.plist", atomically: true)
        
        var arrayM = [UIViewController]()
        for item in array {
            arrayM.append(controller(dict: item as [String : AnyObject]))
        }
        viewControllers = arrayM
    }
    
    private func controller(dict: [String : AnyObject]) -> UIViewController {
        // 取得字典的内容
        guard let clsName = dict["clsName"] as? String,
            let title = dict["title"] as? String,
            let imageName = dict["imageName"] as? String,
            // 使用反射机制 创建控制器
            let cls = NSClassFromString(Bundle.main.namespace + "." + clsName) as?JXBaseViewController.Type,
            let visitorDict = dict["visitorInfo"] as? [String : String]
        else {
            return UIViewController()
        }
        
        // 创建视图控制器
        let vc = cls.init()
        vc.title = title
        
        vc.visitorInfoDictionary = visitorDict
        
        // 设置图像
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        // 设置字体和颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orange], for: .highlighted)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 12)], for: .normal)
        
        let nav = JXNavigationVC(rootViewController: vc)
        
        return nav
    }
}
